package com.stropheware.droidDefense;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

public class Menu {
	
	private static final String TAG = "MenuClass";
	
	private GameBoard gameBoard;
	
	private Context context;
	
	private BasicTower menuBasicTower;
	private AoETower menuAoETower;
	private PowerTower menuPowerTower;
	private SupportTower menuSupportTower;
	private UtilityTower menuUtilityTower;
	
	private int menuWidth;
	
	private int menuHeight;
	
	private int startSidePx;
	
	private float towerPartitionBottomPx;
	
	private float towerPadding;
	
	private float towerMidLeftPx;
	
	public boolean animatingTowerMove;
	
	public Menu(GameBoard gameBoard, Context context) {
		this.gameBoard = gameBoard;
		this.context = context;
		animatingTowerMove = false;
		menuWidth = gameBoard.actualMenuWidth;
		menuHeight = gameBoard.actualHeight;
		startSidePx = gameBoard.actualWidth - menuWidth;
		towerPartitionBottomPx = (menuHeight * 2) / 3;
		towerPadding = (towerPartitionBottomPx - (AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength * 3)) / 5;
		towerMidLeftPx = startSidePx + (menuWidth / 2) - 
			((AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength) / 2);
		menuBasicTower = new BasicTower(context, gameBoard, (int)towerPadding, (int)towerMidLeftPx);
		menuBasicTower.heading = 0;
		menuAoETower = new AoETower(context, gameBoard, (2 * (int)towerPadding) + (AbstractTower.TOWER_SIDE_LENGTH * 
				gameBoard.cellSideLength), (int)towerMidLeftPx);
		menuAoETower.heading = 0;
		menuPowerTower = new PowerTower(context, gameBoard, (3 * (int)towerPadding) +  
				2 *(AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength), (int)towerMidLeftPx);
		menuPowerTower.heading = 0;
		menuSupportTower = new SupportTower(context, gameBoard, (4 * (int)towerPadding) +  
				3 *(AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength), (int)towerMidLeftPx);
		menuSupportTower.heading = 0;
		menuUtilityTower = new UtilityTower(context, gameBoard, (5 * (int)towerPadding) +  
				4 *(AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength), (int)towerMidLeftPx);
		menuUtilityTower.heading = 0;
		if(MainActivity.DEBUG) {
			Log.d(TAG, "towerPadding:" + towerPadding);
			Log.d(TAG, "towerMidLeftPx:" + towerMidLeftPx);
			Log.d(TAG, "towerPartitionBottomPx:" + towerPartitionBottomPx);
		}
	}
	
	public void drawMenu(Canvas canvas) {
		menuBasicTower.drawDrawable(canvas);
		menuAoETower.drawDrawable(canvas);
		menuPowerTower.drawDrawable(canvas);
		menuSupportTower.drawDrawable(canvas);
		menuUtilityTower.drawDrawable(canvas);
		
	}
	
	public boolean touchFirstMenuTower(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(event.getX() >= towerMidLeftPx && event.getX() <= 
				(towerMidLeftPx + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) { 
				if(event.getY() >= towerPadding && event.getY() <= towerPadding + 
						AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean touchSecondMenuTower(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(event.getX() >= towerMidLeftPx && event.getX() <= 
				(towerMidLeftPx + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) { 
				if(event.getY() >= (2 *towerPadding) + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength 
						&& event.getY() <= (2 * towerPadding) + 2 * (AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean touchThirdMenuTower(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(event.getX() >= towerMidLeftPx && event.getX() <= 
				(towerMidLeftPx + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) { 
				if(event.getY() >= (3 *towerPadding) + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength 
						&& event.getY() <= (3 * towerPadding) + 3 * (AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean touchFourthMenuTower(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(event.getX() >= towerMidLeftPx && event.getX() <= 
				(towerMidLeftPx + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) { 
				if(event.getY() >= (4 *towerPadding) + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength 
						&& event.getY() <= (4 * towerPadding) + 4 * (AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean touchFifthMenuTower(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(event.getX() >= towerMidLeftPx && event.getX() <= 
				(towerMidLeftPx + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) { 
				if(event.getY() >= (5 *towerPadding) + AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength 
						&& event.getY() <= (5 * towerPadding) + 5 * (AbstractTower.TOWER_SIDE_LENGTH * gameBoard.cellSideLength)) {
					return true;
				}
			}
		}
		return false;
	}
}
